/*
 * Copyright (C) 2012 Fabien Barbero
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights 
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.flickr.activities;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import com.flickr.api.FlickrServiceException;
import com.flickr.api.Paginated;
import com.flickr.api.auth.OAuthHandler;
import com.flickr.api.photos.Photo;
import com.flickr.api.photosets.Photoset;
import com.flickr.api.photosets.PhotosetsService;
import com.flickr.api.photosets.PhotosetsServiceImpl;
import com.flickr.callbacks.PhotosLoaderCallback;


public class PhotosetGalleryActivity extends AbstractGalleryActivity
{

    public static final String PHOTOSET_KEY = "photoset";
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        
        OAuthHandler oauthHandler = OAuthHandler.getHandler();
        PhotosetsService photosetsService = new PhotosetsServiceImpl(oauthHandler);
        
        Intent intent = getIntent();
        Photoset set = (Photoset) intent.getSerializableExtra(PHOTOSET_KEY);
        
        new PhotosetGalleryPhotosLoadTask(photosetsService, set, this).execute();
    }
    
    
    private static class PhotosetGalleryPhotosLoadTask extends AsyncTask<Void, Void, Paginated<Photo>> {
        
        private static final String TAG = PhotosetGalleryPhotosLoadTask.class.getSimpleName();

        private final PhotosetsService mPhotosetsService;
        private final Photoset mSet;
        private final PhotosLoaderCallback mCallback;

        private PhotosetGalleryPhotosLoadTask(PhotosetsService photosetsService, Photoset set, PhotosLoaderCallback callback) {
            mPhotosetsService = photosetsService;
            mSet = set;
            mCallback = callback;
        }
        
        @Override
        protected Paginated<Photo> doInBackground(Void... params)
        {
            try {
                return mPhotosetsService.getPhotos(mSet, Integer.MAX_VALUE, 0);
            } catch (FlickrServiceException e) {
                Log.e(TAG, "Error loading photoset photos", e);
                return null;
            }
        }
        
        @Override
        protected void onPostExecute(Paginated<Photo> result)
        {
            if(result != null) {
                mCallback.onPhotosLoaded(result);
            }
        }
        
    }
    
}
